﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using RemoteController;

namespace VisualRemoteController
{
    /// <summary>
    /// Interaction logic for RemoteControlGUI.xaml
    /// </summary>
    public partial class RemoteControlGUI : Window
    {
        List<ComboBox> ComboBoxList = new List<ComboBox>();
        List<Command> CommandOnList = new List<Command>();
        List<Command> CommandOffList = new List<Command>();

        Command lastCommand = null;
        
        public RemoteControlGUI()
        {
            InitializeComponent();

            ComboBoxList.Add(cmbCommandList0);
            ComboBoxList.Add(cmbCommandList1);
            ComboBoxList.Add(cmbCommandList2);
            ComboBoxList.Add(cmbCommandList3);
            ComboBoxList.Add(cmbCommandList4);

            foreach (ComboBox c in ComboBoxList)
            {
                c.Items.Add("No command");
                c.Items.Add("Ceiling Fan");
                c.Items.Add("Tv");
                c.Items.Add("Stereo");
                c.Items.Add("Hottub");
                c.Items.Add("Garage");
                c.Items.Add("Any given light bulb");
                c.Items.Add("Living room light");
                c.Items.Add("Bathroom light");
                c.Items.Add("DVD");
                c.Items.Add("Bathroom stereo");
                c.Items.Add("Party Mode");
                c.Items.Add("Movie Mode");
                c.Items.Add("Relax Mode");
                c.Items.Add("Ya llegue");

                c.SelectedIndex = 0;
            }

            CeilingFan ceilingFan = new CeilingFan("Living room");
            TV tv = new TV("Living room");
            Stereo stereo = new Stereo("Living room");
            Hottub hottub = new Hottub();
            GarageDoor garageDoor = new GarageDoor("Garage");
            Light lightBulb = new Light("Any given light bulb");            
            Light livingRoomLights = new Light("Living room");
            Light bathroomLight = new Light("Bathroom");
            DVD dvd = new DVD("Living room");
            Stereo bathroomstereo = new Stereo("Bathroom");
            Yallegue yallegue = new Yallegue(garageDoor, livingRoomLights, tv,
                new GUICommandDecoratorStrategy[] {
                new GUICommandDecoratorImageStrategy(garageDoorImage),
                new GUICommandDecoratorImageStrategy(livingRoomLightsImage),
                new GUICommandDecoratorImageStrategy(tvImage)});

            //                          tvOn, stereoOn, dvdOn, lightOff
            GUICommandDecoratorImageStrategy[] partyStrategy = new GUICommandDecoratorImageStrategy[] { new GUICommandDecoratorImageStrategy(livingRoomLightsImage), new GUICommandDecoratorImageStrategy(stereoImage), new GUICommandDecoratorImageStrategy(ceilingFanImage) };
            GUICommandDecoratorImageStrategy[] movieStrategy = new GUICommandDecoratorImageStrategy[] { 
                new GUICommandDecoratorImageStrategy(tvImage), 
                new GUICommandDecoratorImageStrategy(stereoImage), 
                new GUICommandDecoratorImageStrategy(dvdImage) ,
                new GUICommandDecoratorImageStrategy(livingRoomLightsImage)};
            GUICommandDecoratorImageStrategy[] relaxStrategy = new GUICommandDecoratorImageStrategy[] { new GUICommandDecoratorImageStrategy(bathroomLightImage), new GUICommandDecoratorImageStrategy(bathroomstereoImage), new GUICommandDecoratorImageStrategy(hottubImage) };

            PartyMacroCommandGetterO party = new PartyMacroCommandGetterO(livingRoomLights, stereo, ceilingFan, partyStrategy);
            MovieMacroCommandGetterO movie = new MovieMacroCommandGetterO(livingRoomLights, tv, stereo, dvd, movieStrategy);
            RelaxMacroCommandGetterO relax = new RelaxMacroCommandGetterO(bathroomLight, bathroomstereo, hottub, relaxStrategy);
            /*
            WindowImage ceilingFanWindowImage = new WindowImage("Ceiling Fan");
            WindowImage tvWindowImage = new WindowImage("Tv");
            WindowImage stereoWindowImage = new WindowImage("Stereo");
            WindowImage hottubWindowImage = new WindowImage("Hottub");
            WindowImage garageDoorWindowImage = new WindowImage("Garage Door");
            WindowImage lightBulbWindowImage = new WindowImage("Light Bulb");
            WindowImage livingRoomLightsWindowImage = new WindowImage("Living Room Lights");
            WindowImage bathroomLightWindowImage = new WindowImage("Bathroom light");
            */

            CommandOnList.Add(new NoCommand());
            CommandOnList.Add(new GUICommandDecorator(
                new CeilingFanOnCommand(ceilingFan),
                new GUICommandDecoratorImageStrategy(ceilingFanImage)
                ));
            CommandOnList.Add(new GUICommandDecorator(
                new TVOnCommand(tv),
                new GUICommandDecoratorImageStrategy(tvImage)
                ));
            CommandOnList.Add(new GUICommandDecorator(
                new StereoOnCommand(stereo),
                new GUICommandDecoratorImageStrategy(stereoImage)
                ));
            CommandOnList.Add(new GUICommandDecorator(
                new HottubOnCommand(hottub),
                new GUICommandDecoratorImageStrategy(hottubImage)
                ));
            CommandOnList.Add(new GUICommandDecorator(
                new GarageDoorUpCommand(garageDoor),
                new GUICommandDecoratorImageStrategy(garageDoorImage)
                ));
            CommandOnList.Add(new GUICommandDecorator(
                new LightOnCommand(lightBulb),
                new GUICommandDecoratorImageStrategy(lightBulbImage)
                ));
            CommandOnList.Add(new GUICommandDecorator(
                new LivingroomLightOnCommand(livingRoomLights),
                new GUICommandDecoratorImageStrategy(livingRoomLightsImage)
                ));
            CommandOnList.Add(new GUICommandDecorator(
                new BathroomLightOnCommand(bathroomLight),
                new GUICommandDecoratorImageStrategy(bathroomLightImage)
                ));
           CommandOnList.Add(new GUICommandDecorator(
                new DVDOnCommand(dvd),
                new GUICommandDecoratorImageStrategy(dvdImage)
                ));
           CommandOnList.Add(new GUICommandDecorator(
                new StereoOnCommand(bathroomstereo),
                new GUICommandDecoratorImageStrategy(bathroomstereoImage)
                ));

           CommandOnList.Add(party.getOnMacro());
           CommandOnList.Add(movie.getOnMacro());
           CommandOnList.Add(relax.getOnMacro());
           CommandOnList.Add(yallegue.getOnMacro());
            


            CommandOffList.Add(new NoCommand());
            CommandOffList.Add(new GUICommandDecorator(
                new CeilingFanOffCommand(ceilingFan),
                new GUICommandDecoratorImageStrategy(ceilingFanImage)
                ));
            CommandOffList.Add(new GUICommandDecorator(
                new TVOffCommand(tv),
                new GUICommandDecoratorImageStrategy(tvImage)
                ));
            CommandOffList.Add(new GUICommandDecorator(
                new StereoOffCommand(stereo),
                new GUICommandDecoratorImageStrategy(stereoImage)
                ));
            CommandOffList.Add(new GUICommandDecorator(
                new HottubOffCommand(hottub),
                new GUICommandDecoratorImageStrategy(hottubImage)
                ));
            CommandOffList.Add(new GUICommandDecorator(
                new GarageDoorDownCommand(garageDoor),
                new GUICommandDecoratorImageStrategy(garageDoorImage)
                ));
            CommandOffList.Add(new GUICommandDecorator(
                new LightOffCommand(lightBulb),
                new GUICommandDecoratorImageStrategy(lightBulbImage)
                ));
            CommandOffList.Add(new GUICommandDecorator(
                new LivingroomLightOffCommand(livingRoomLights),
                new GUICommandDecoratorImageStrategy(livingRoomLightsImage)
                ));
            CommandOffList.Add(new GUICommandDecorator(
                new BathroomLightOffCommand(bathroomLight),
                new GUICommandDecoratorImageStrategy(bathroomLightImage)
                ));
            CommandOffList.Add(new GUICommandDecorator(
                new DVDOffCommand(dvd),
                new GUICommandDecoratorImageStrategy(dvdImage)
                ));
            CommandOffList.Add(new GUICommandDecorator(
                new StereoOffCommand(stereo),
                new GUICommandDecoratorImageStrategy(bathroomstereoImage)
                ));
            CommandOffList.Add(party.getOffMacro());
            CommandOffList.Add(movie.getOffMacro());
            CommandOffList.Add(relax.getOffMacro());
            CommandOffList.Add(yallegue.getOffMacro());
        }

        private void btnOn_Click(object sender, RoutedEventArgs e)
        {
            if (!(sender is Button) )
                return;

            int buttonNumber = getButtonNumber((Button)sender);
            int selectedIndex = ComboBoxList[buttonNumber].SelectedIndex;

            CommandOnList[selectedIndex].Execute();
            lastCommand = CommandOnList[selectedIndex];
        }

        private void btnOff_Click(object sender, RoutedEventArgs e)
        {
            if (!(sender is Button))
                return;

            int buttonNumber = getButtonNumber((Button)sender);
            int selectedIndex = ComboBoxList[buttonNumber].SelectedIndex;

            CommandOffList[selectedIndex].Execute();
            lastCommand = CommandOffList[selectedIndex];
        }

        private int getButtonNumber(Button b)
        {
            String name = b.Name.ToString();
            int i = Int32.Parse(name.Substring(name.Length - 1, 1));
            return i;
        }

        private void btnUndo_Click(object sender, RoutedEventArgs e)
        {
            if (lastCommand == null)
                return;
            lastCommand.Undo();
        }
    }
}
